﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebApiV4Test.Data.Entity;
using WebApiV4Test.Domain;
using WebApiV4Test.Domain.Enum;
using WebApiV4Test.Interface.Log;

namespace WebApiV4Test.Implement.Log
{
    public class Log:ILog
    {
        private readonly IRespository<Logs> _logsrespository;
        public Log(IRespository<Logs> LogsRespository)
        {
            _logsrespository = LogsRespository;
        }
        public Logs GetLogById(int id)
        {
            return _logsrespository.GetById(id);

        }

        public void Clear()
        {
            var list = _logsrespository.Table.ToList();
            _logsrespository.Delete(list);
        }

        public void Delete(Logs log)
        {
            _logsrespository.Delete(log);
        }

        public void Insert(Logs log)
        {
            _logsrespository.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogsLevel logsLevel = LogsLevel.错误, int usersId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;
            var list = _logsrespository.Table.Where(x => x.CreateTime >= bTime && x.CreateTime <= eTime && x.LogLevel == (int)logsLevel);
            if (usersId != 0)
            {
                list = list.Where(x => x.UserId == usersId);
            }
            return list;
        }
    }
}
